import { downloadFile, queryUrlParams } from '@/utils/index'
import request from '@/utils/request'

export function searchDrug(name, pharmacyId, drugType) {
  const url = 'drug/getDrugs?page=0&size=10&status=0&drugName=' + name + '&pharmacyId=' + pharmacyId + '&drugType=' + drugType
  return request({
    url: url,
    method: 'get'
  })
}

export function getSuppliers() {
  const url = 'api/supplier/getSuppliers?page=0&size=50&status=0'
  return request({
    url: url,
    method: 'get'
  })
}

export function add(data) {
  return request({
    url: 'drugWareHousing/add',
    method: 'post',
    data
  })
}

export function detail(id) {
  return request({
    url: 'drugWareHousing/detail?id=' + id,
    method: 'get'
  })
}

export function cancelWarehousing(id) {
  return request({
    url: 'drugWareHousing/update?id=' + id,
    method: 'post'
  })
}

export function outList(data) {
  const param = queryUrlParams(data)
  request({
    url: 'drugWareHousing/outList?' + param,
    method: 'get',
    responseType: 'blob'
  }).then(result => {
    if (data.type === 1) {
      downloadFile(result, '采购单', 'xlsx')
    } else {
      downloadFile(result, '入库单', 'xlsx')
    }
  })
}

export function outTemplate(data) {
  let url = 'drugWareHousing/outTemplate'
  if (data.drugType) {
    url += '?drugType=' + data.drugType
  }
  request({
    url: url,
    method: 'get',
    responseType: 'blob'
  }).then(result => {
    if (data.type === 1) {
      downloadFile(result, '采购单', 'xlsx')
    } else {
      downloadFile(result, '入库单', 'xlsx')
    }
  })
}

// 入库/采购列表
export function getList(data) {
  const param = queryUrlParams(data)
  return request({
    url: 'drugWareHousing/getList?' + param,
    method: 'get'
  })
}

// 修改付款状态
export function updatePayStatus(data) {
  const param = queryUrlParams(data)
  return request({
    url: 'drugWareHousing/updatePayStatus?' + param,
    method: 'get'
  })
}

// 导出入库/采购单
export function outWareHousing(data, type) {
  const param = queryUrlParams(data)
  request({
    url: 'drugWareHousing/outWareHousing?' + param,
    method: 'get',
    responseType: 'blob'
  }).then(result => {
    if (type === 1) {
      downloadFile(result, '采购单', 'xlsx')
    } else {
      downloadFile(result, '入库单', 'xlsx')
    }
  })
}

// 导出入库清单
export function outDetail(data) {
  const param = queryUrlParams(data)
  request({
    url: 'drugWareHousing/outDetail?' + param,
    method: 'get',
    responseType: 'blob'
  }).then(result => {
    downloadFile(result, '入库清单', 'xlsx')
  })
}
